package com.atguigu.gmall.mq.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.service.RabbitService;
import com.atguigu.gmall.mq.config.DeadLetterMqConfig;
import com.atguigu.gmall.mq.config.DelayedMqConfig;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * mq测试代码
 */
@RestController
@RequestMapping("/admin/mq")
public class MqController {

    @Autowired
    private RabbitService rabbitService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送普通消息
     * @param msg 消息
     * @return 结果
     */
    @GetMapping("/sendMsg")
    public Result sendMsg(@RequestParam("msg") String msg){
        String exchange = "exchange.confirm";
        String routingKey = "routing.confirm";
        rabbitService.sendMessage(exchange, routingKey, msg);
        return Result.ok();
    }

    /**
     * 死信队列实现延迟消息
     * @param msg
     * @return
     */
    @GetMapping("/sendDeadLetterMsg")
    public Result sendDeadLetterMsg(@RequestParam("msg") String msg){
        //发送延迟消息
        rabbitTemplate.convertAndSend(DeadLetterMqConfig.exchange_dead,DeadLetterMqConfig.routing_dead_1,msg);
        return Result.ok();
    }

    /**
     * 延迟插件实现延迟消息
     * @param msg
     * @param ttl
     * @return
     */
    @GetMapping("/sendDelayMsg")
    public Result sendDelayMsg(@RequestParam("msg") String msg,@RequestParam("ttl") Integer ttl){
        rabbitService.sendDelayMessage(DelayedMqConfig.exchange_delay,DelayedMqConfig.routing_delay,msg,ttl);
        return Result.ok();
    }
}